//
//  DiscoverHouseholdListViewController.m
//  HJB
//
//  Created by jiezsoft.com on 14-10-10.
//  Copyright (c) 2014年 jiezsoft.com. All rights reserved.
//

#import "DiscoverHouseholdListViewController.h"
#import "HouseholdDetailViewController.h"
#import "UIViewExt.h"
#import "HttpRequest.h"
#import "UIButton+WebCache.h"
#import "RDVTabBarController.h"
//#import "MJRefresh.h"
#import "HouseholdListCell.h"
#import "LoginViewController.h"
//#import "ToBindCompanyViewController.h"
#import "LoginCompanysViewController.h"
#import "DiscoverListModel.h"
#import "LXSegmentTitleView.h"
#import "LXScrollContentView.h"

#import "DiscoverLisctViewController.h"
#import "ClassificationSearchView.h"

//#import "TSFooterRefresh.h"
//#import "TSHeaderRefresh.h"

#import "DiscoverSearchViewController.h"
@interface DiscoverHouseholdListViewController ()<UITextFieldDelegate, LXSegmentTitleViewDelegate,LXScrollContentViewDelegate>
@property (strong, nonatomic) NSString *ImageUrlStr;
@property (strong, nonatomic) NSString*contentStr;
@property (strong, nonatomic) NSString*titleStr;
@property (nonatomic, strong) LXSegmentTitleView *titleView;
@property (nonatomic, strong) LXScrollContentView *contentView;
@property (nonatomic, assign) NSInteger index;
@end
@implementation DiscoverHouseholdListViewController
{
    NSMutableArray*modelArray;
    NSMutableArray*codeArray;
    NSMutableArray*contentArray;
    int pageNumber;
    NSString *result;
    NSString*TotalCount;
    UITableView *table;
    UIView*noDataView;
    UIButton *userStatusBtn;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"发现";
    
    modelArray = [NSMutableArray array];
    codeArray=[[NSMutableArray alloc]init];
    contentArray=[[NSMutableArray alloc]init];
    self.index = 0;
    
    // 滑动头部
    self.titleView = [[LXSegmentTitleView alloc] initWithFrame:CGRectZero];
    self.titleView.itemMinMargin = 10.f;
    self.titleView.frame = CGRectMake(0, 0, ScreenWidth, 45);
    self.titleView.delegate = self;
    self.titleView.selectedIndex = 0;
    self.titleView.backgroundColor = zuiqianhuiColor;
    self.titleView.indicatorColor = lanse;
    self.titleView.titleNormalColor =[UIColor colorWithHexString:@"999999"];
    self.titleView.titleSelectedColor =lanse4;
    [self.view addSubview:self.titleView];
    
    // 中部内容
    self.contentView = [[LXScrollContentView alloc] initWithFrame:CGRectZero];
    self.contentView.frame = CGRectMake(0, self.titleView.bottom+1, ScreenWidth, ScreenHeight -TabbarHeight-StatusBarAndNavigationBarHeight-45);
    self.contentView.delegate = self;
    [self.view addSubview:self.contentView];
    
//    modelArray = [NSMutableArray array];
//    codeArray=[[NSMutableArray alloc]init];
//    contentArray=[[NSMutableArray alloc]init];
//    self.index = 0;
//
//
//
//    UIView *topView = [[UIView alloc] init];
//    topView.frame = CGRectMake(0, 0, ScreenWidth, 64);
//    topView.backgroundColor = lanse;
//    [self.view addSubview:topView];
//
////    UIView *searchView = [[UIView alloc] init];
////    searchView.frame = CGRectMake(0, 20, ScreenWidth, 44);
////    searchView.backgroundColor = lanse;
////    [topView addSubview:searchView];
//
//    // done --> 完成
////    [IQKeyboardManager sharedManager].toolbarDoneBarButtonItemText = @"搜索";
////    // 搜索view
////    ClassificationSearchView *cv = [ClassificationSearchView classificationNavBar];
////    //    cv.delegate = self;
////    //    cv.frame = CGRectMake(30, 0, ScreenWidth- 40 *2, 30);
////    cv.frame = CGRectMake(30, 20+ ((44-30)*0.5), ScreenWidth - 30*2, 30);
////    cv.searchWoldTF.delegate = self;
////    cv.searchWoldTF.returnKeyType = UIReturnKeySearch;
////    [topView addSubview:cv];
//
//
//
//
//    self.titleView = [[LXSegmentTitleView alloc] initWithFrame:CGRectZero];
////    self.titleView.frame = CGRectMake(0, topView.bottom, ScreenWidth, <#CGFloat height#>)
//    self.titleView.itemMinMargin = 10.f;
//    self.titleView.backgroundColor = [UIColor whiteColor];
//    self.titleView.frame = CGRectMake(0, topView.bottom, ScreenWidth, 45);
//    self.titleView.delegate = self;
////    NSArray *titles = @[@"首页",@"体育在线",@"科技日报",@"生活",@"本地"];
////    self.titleView.segmentTitles = titles;
//
//    self.titleView.selectedIndex = 0;
//    self.titleView.backgroundColor = [UIColor whiteColor];
//    self.titleView.indicatorColor = lanse;
//    self.titleView.titleNormalColor = [UIColor colorWithHexString:@"666666"];
//    self.titleView.titleSelectedColor =lanse;
//    [self.view addSubview:self.titleView];
//
//
//    self.contentView = [[LXScrollContentView alloc] initWithFrame:CGRectZero];
//    self.contentView.frame = CGRectMake(0, self.titleView.bottom+1, ScreenWidth, ScreenHeight -49-64);
//    self.contentView.delegate = self;
//    self.contentView.backgroundColor = [UIColor redColor];
//    [self.view addSubview:self.contentView];
//
//
//
//
//
//
////    UIView *lineView1 = [[UIView alloc] init];
////    lineView1.frame = CGRectMake(0, 0, ScreenWidth, 20.0);
////    lineView1.backgroundColor = lanse;
////    [self.view addSubview:lineView1];
//
////    UIView *lineView = [[UIView alloc] init];
////    lineView.frame = CGRectMake(0, self.titleView.bottom, ScreenWidth, 1.0);
////    lineView.backgroundColor = [UIColor colorWithHexString:@"eeeeee"];
////    [self.view addSubview:lineView];
    
    [self getAppCodes];
}

- (void)searchBtnClick:(UIButton *)sender{
    DiscoverSearchViewController *vc = [[DiscoverSearchViewController alloc] init];
    vc.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController pushViewController:vc animated:YES];
    
}

// 获取title
- (void)getAppCodes {
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    // InfoType  //InfoSource
    [param setValue:@"InfoType" forKey:@"optionName"];
    [MBProgressHUD showIndeterminateHudSub:@"" delay:30 view:self.view];
    [HttpRequest requestData:@"System/AppCodes" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        NSLog(@"====%@",severDataDict.dataDict);
        if (isSuccess) {
            [contentArray removeAllObjects];
            [codeArray removeAllObjects];
            
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [MBProgressHUD hideProgressHUDInternal:self.view];
                NSArray *arrary1 = [NSArray array];
                for (NSDictionary* dicc in [severDataDict.dataDict objectForKey:@"DataLine"] ) {
                    [codeArray addObject:[dicc valueForKey:@"code"]];
                    [contentArray addObject:[dicc valueForKey:@"content"]];
                }
            
//                [codeArray addObject:@"123"];
//                [contentArray addObject:@"今天"];
//
//                [codeArray addObject:@"456"];
//                [contentArray addObject:@"我的"];
//
//                [codeArray addObject:@"789"];
//                [contentArray addObject:@"网易"];
//
//                [codeArray addObject:@"101"];
//                [contentArray addObject:@"大大"];
                arrary1 = contentArray;
                
                self.titleView.segmentTitles = arrary1;
                NSMutableArray *vcs = [[NSMutableArray alloc] init];
                for (NSString *title in codeArray) {
                    DiscoverLisctViewController *vc = [[DiscoverLisctViewController alloc] init];
                    vc.dhTypeCode = title;
                    [vcs addObject:vc];
                }
                [self.contentView reloadViewWithChildVcs:vcs parentVC:self];
                self.titleView.selectedIndex = self.index;
                self.contentView.currentIndex = self.index;
            }else{
                
                [MBProgressHUD hideProgressHUDInternal:self.view];
                
            }
            
        } else {
            
            [MBProgressHUD hideProgressHUDInternal:self.view];
            [MBProgressHUD showTextHudSub:severDataDict.resultStr view:self.view];
        }
        
    }];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"发现";
    }
    return self;
}
- (void)viewDidDisappear:(BOOL)animated {
    
    [super viewDidDisappear:animated];
//    self.navigationController.delegate = nil;
    
}
- (void)viewDidAppear:(BOOL)animated {
    
    [super viewDidAppear:animated];
    // 禁用 iOS7 返回手势
//    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
//        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
//    }
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"changeTokenId" object:nil];
}
-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [[self rdv_tabBarController] setTabBarHidden:NO];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(ChangeToken) name:@"changeTokenId" object:Nil];
    
    self.navigationController.navigationBarHidden=NO;
    
    
    
    UIImage *img=[UIImage imageNamed:@"sousuo"];
    UIImage *tinted = [img rt_tintedImageWithColor:[UIColor whiteColor]];
    UIButton *searchBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 0.0, 60/2, 60/2)];
    [searchBtn setImage:tinted forState:UIControlStateNormal];
    [searchBtn setImage:tinted forState:UIControlStateHighlighted];
    [searchBtn addTarget:self action:@selector(searchBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* searchBtnItem = [[UIBarButtonItem alloc] initWithCustomView:searchBtn];
    self.navigationItem.rightBarButtonItem = searchBtnItem;
    
//    [userStatusBtn removeFromSuperview];
//    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
//    if ([[user objectForKey:@"userStatus"] isEqualToString:@"W"]) {
//        userStatusBtn=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/2-60,ScreenHeight/2-50, 120, 25)];
//        [userStatusBtn setTitle:@"认证中...请等待！" forState:UIControlStateNormal];
//        userStatusBtn.backgroundColor=luseColor;
//        [userStatusBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//        userStatusBtn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
//        [userStatusBtn addTarget:self action:@selector(userStatus) forControlEvents:UIControlEventTouchUpInside];
//        userStatusBtn.titleLabel.font=[UIFont systemFontOfSize:15];
//        [self.view addSubview:userStatusBtn];
//
//    }else if ([[user objectForKey:@"userStatus"] isEqualToString:@"E"]) {
//        userStatusBtn=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/2-60,ScreenHeight/2-50, 120, 25)];
//        [userStatusBtn setTitle:@"去认证门店先！" forState:UIControlStateNormal];
//        userStatusBtn.backgroundColor=luseColor;
//        [userStatusBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//        userStatusBtn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
//        userStatusBtn.titleLabel.font=[UIFont systemFontOfSize:15];
//        [userStatusBtn addTarget:self action:@selector(userStatus) forControlEvents:UIControlEventTouchUpInside];
//        [self.view addSubview:userStatusBtn];
//    }

    [self ChangeToken];
//    [self userStatus];
    
    
    
}



- (void)segmentTitleView:(LXSegmentTitleView *)segmentView selectedIndex:(NSInteger)selectedIndex lastSelectedIndex:(NSInteger)lastSelectedIndex{
    self.contentView.currentIndex = selectedIndex;
    self.index = selectedIndex;
}

- (void)contentViewDidScroll:(LXScrollContentView *)contentView fromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex progress:(float)progress{
    
}

- (void)contentViewDidEndDecelerating:(LXScrollContentView *)contentView startIndex:(NSInteger)startIndex endIndex:(NSInteger)endIndex{
    self.titleView.selectedIndex = endIndex;
    self.index = endIndex;
}



-(void)ChangeToken {
//    [self requestData];
    [self getAppCodes];
}
//-(void)userStatus{
//    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
//    if ([[user objectForKey:@"userStatus"] isEqualToString:@"E"]) {
////        ToBindCompanyViewController * Login=[[ToBindCompanyViewController alloc]init];
////        Login.viewSat=@"The original road";
////        [self.navigationController pushViewController:Login animated:YES];
//    }else if ([[user objectForKey:@"userStatus"] isEqualToString:@"W"]) {
//        LoginCompanysViewController * Login=[[LoginCompanysViewController alloc]init];
//        [self.navigationController pushViewController:Login animated:YES];
//    }
//}

@end
